<?php
namespace app\admin\controller;

use think\Loader;
use think\Request;
use app\common\controller\Base;
use app\common\tool\Tool;
use app\common\model\SolutionCat as SolutionCatModel;


class SolutionCat extends Base
{
    public function index(){

        $solutionCats = SolutionCatModel::order('id desc')->paginate(20);

        return view('',['solutionCats'=>$solutionCats]);
    }


    public function add(){

        return view();
    }


    public function  create(Request $request){
        if(!$request->isPost()){
            $this->error("非法操作");
        }
        $data = $request->only(['title','seo_title','seo_keywords','seo_description','desc','cover']);

        $validate = Loader::validate('SolutionCatValidate');
        $result = $validate-> scene('add')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $solutionCatModel = new SolutionCatModel();
        $solutionCatModel ->save($data);

        $this->success('添加成功');
    }


    public function edit(Request $request){

        $id = $request->param('id',0,'intval');
        if(!$id){
            $this->error('非法来源');
        }

        $solutionCat = SolutionCatModel::find($id);

        return view('',['solutionCat'=>$solutionCat]);
    }

    public function update(Request $request){
        if(!$request->isPost()){
            $this->error("非法操作");
        }
        $id = $request -> param('id');
        $data = $request->only(['title','seo_title','seo_keywords','seo_description','desc','cover']);

        $validate = Loader::validate('SolutionCatValidate');
        $result = $validate-> scene('edit')->check($data);
        if(!$result){
            $this->error($validate->getError());
        }

        $solutionCatModel = SolutionCatModel::find($id);
        $solutionCatModel ->save($data);

        $this->success('更新成功');
    }


    public function delete(Request $request){
        if(!$request->isDelete()){
            $this->error("非法操作");
        }
        $id = $request -> param('id');
        SolutionCatModel::destroy($id);
        $this->success("删除成功");
    }

}
